package com.begye.smartloan.apptest.xieyi

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.begye.smartloan.apptest.MyApplication
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.AtXieYiBinding
import com.begye.smartloan.apptest.kefu.KeFuDialogFragment
import com.begye.smartloan.apptest.tool.addTouch

class XieYiActivity : AppCompatActivity() {

    private lateinit var _binding: AtXieYiBinding

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        // 关联布局
        _binding = AtXieYiBinding.inflate(layoutInflater)
        setContentView(_binding.root)

        _binding.atXieYiNar.initConfig(
            text = getString(R.string.chou_ti_text_3),
            textColor = "#000000",
            leftResId = R.drawable.dres_39,
            rightResId = R.drawable.dres_38
        ) {
            if (0 == it) {
                finish()
            } else if (1 == it) {
                val dialog = KeFuDialogFragment()
                dialog.showNow(supportFragmentManager, "kefu")
            }
        }

        // 初始化
        initWebViewSettings()
    }


    @SuppressLint("SetJavaScriptEnabled")
    private fun initWebViewSettings() {
        _binding.atXieYiWebView.setInitialScale(100)
        _binding.atXieYiWebView.webChromeClient = mWebChromeClient
        _binding.atXieYiWebView.webViewClient = mWebClient
        val webSettings = _binding.atXieYiWebView.settings
        webSettings.setSupportZoom(true)
        try {
            webSettings.javaScriptEnabled = true
            webSettings.textZoom = 100
            webSettings.domStorageEnabled = true
            webSettings.builtInZoomControls = true
            webSettings.useWideViewPort = true
            webSettings.loadWithOverviewMode = true
            webSettings.cacheMode = WebSettings.LOAD_NO_CACHE
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)
            webSettings.allowUniversalAccessFromFileURLs = true
            webSettings.allowFileAccessFromFileURLs = true
            webSettings.allowContentAccess = true
        } catch (e: Exception) {
            e.printStackTrace()
        }

        _binding.atXieYiWebView.loadUrl(MyApplication.policyLink)
    }

    private val mWebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            super.onProgressChanged(view, newProgress)

            _binding.atXieYiProgressBar.progress = newProgress
            if (newProgress == 100) {
                _binding.atXieYiProgressBar.visibility = View.GONE
            } else {
                _binding.atXieYiProgressBar.visibility = View.VISIBLE
            }
        }
    }

    private val mWebClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?,
        ): Boolean {
            return shouldOverrideUrlLoading(view, request!!.url.toString())
        }

        override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
            return false
        }
    }

    override fun onResume() {
        super.onResume()

        addTouch(type = this.javaClass.simpleName, isOpen = true)
    }

    override fun onPause() {
        super.onPause()

        addTouch(type = this.javaClass.simpleName, isComplete = true)
    }
}